package com.tong.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tong.wms.config.QueryPageParam;
import com.tong.wms.config.Result;
import com.tong.wms.pojo.Menu;
import com.tong.wms.pojo.User;
import com.tong.wms.service.MenuService;
import com.tong.wms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public List<User> list() {
        return userService.listAll();
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        List<User> list = userService.lambdaQuery()
                .eq(User::getNo, user.getNo())
                .eq(User::getPassword, user.getPassword()).list();
        if (list.size() > 0) {
            User user1 = list.get(0);
            List<Menu> menuList = menuService.lambdaQuery()
                    .like(Menu::getMenuRight, user1.getRole_id()).list();
            HashMap res = new HashMap();
            res.put("user", user1);
            res.put("menu", menuList);
            return Result.success(res);
        }
        return Result.fail();
    }

    @PostMapping("/findByNo")
    public Result findByNo(@RequestParam String no) {
        List<User> list = userService.lambdaQuery().eq(User::getNo, no).list();
        return list.size() > 0 ? Result.success(list) : Result.fail();
    }

    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.success() : Result.fail();
    }

    @PostMapping("/mod")
    public boolean modify(@RequestBody User user) {
        return userService.updateById(user);
    }

    @PostMapping("/saveOrMod")
    public Result SaveOrUpdate(@RequestBody User user) {
        return userService.saveOrUpdate(user) ? Result.success() : Result.fail();
    }

    @DeleteMapping("/del")
    public Result delete(@RequestParam int id) {
        return userService.removeById(id) ? Result.success() : Result.fail();
    }

    // 模糊查询
    @PostMapping("/select")
    public Result select(@RequestBody User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询
        if (StringUtils.isNotBlank(user.getName())) {
            queryWrapper.like(User::getName, user.getName());
        }
        // 精准查询
        //queryWrapper.eq(User::getName,user.getName());
        return Result.success(userService.list(queryWrapper));
    }

    //封装入参
//    @PostMapping("/page")
//    public List<User> listByPage(@RequestBody QueryPageParam query){
//        HashMap param = query.getParam();
//        String name = (String) param.get("name");
//        Integer age = (Integer) param.get("age");
//        System.out.println(name+":"+age);
//        return null;
//    }
/*    @PostMapping("/page")
    public List<User> listByPage(@RequestBody QueryPageParam query){
        Page<User> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        IPage<User> userPage = userService.diyPage(page);
        long total = userPage.getTotal();
        System.out.println(total);
        return userPage.getRecords();
    }*/


    @PostMapping("/page")
    public Result listByPage(@RequestBody QueryPageParam query) {
        HashMap map = query.getParam();
        String name = (String) map.get("name");
        String sex = (String) map.get("sex");
        String role_id = (String) map.get("role_id");

        Page<User> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            queryWrapper.like(User::getName, name);
        }
        if (StringUtils.isNotBlank(sex)) {
            queryWrapper.eq(User::getSex, sex);
        }
        if (StringUtils.isNotBlank(role_id)) {
            queryWrapper.eq(User::getRole_id, role_id);
        }

        IPage<User> userPage = userService.diyPage2(page, queryWrapper);
        long total = userPage.getTotal();
        System.out.println(total);

        //return Result.success(userService.list(queryWrapper), total);
        return Result.success(userPage.getRecords(), userPage.getTotal());
    }

    @PostMapping("/result")
    public Result result(@RequestBody QueryPageParam query) {
        HashMap map = query.getParam();
        String name = (String) map.get("name");

        Page<User> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getName, name);

        IPage<User> userPage = userService.diyPage2(page, queryWrapper);
        long total = userPage.getTotal();
        System.out.println(total);
        return Result.success(userPage.getRecords(), userPage.getTotal());
    }
}
